package com.applcn.core.config;

import com.applcn.core.filter.CrossFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.servlet.Filter;
import java.util.ArrayList;
import java.util.List;

/**
 * com.applcn.core.config.CorsConfig
 * Description:
 *
 * @author: 大妖怪
 * @date: 2020-1-14 22:51
 */
@Configuration
public class CorsConfig implements WebMvcConfigurer {

    /**
     * 注册跨域filter
     */
    @Bean
    public FilterRegistrationBean<Filter> crossDomainFilter() {
        FilterRegistrationBean<Filter> registrationBean = new FilterRegistrationBean<>();
        //注入过滤器
        registrationBean.setFilter(new CrossFilter());
        //拦截规则
        List<String> urlPatterns = new ArrayList<>();
        urlPatterns.add("/*");
        registrationBean.setUrlPatterns(urlPatterns);
        //过滤器名称
        registrationBean.setName("crossFilter");
        //过滤器顺序
        registrationBean.setOrder(FilterRegistrationBean.HIGHEST_PRECEDENCE);
        return registrationBean;
    }
}